#include <iostream>
using namespace std;
#include <pthread.h>
#include <unistd.h>

/* 票数:10 */
int g_count = 10;

void *thread_func1(void *arg)
{
    while (g_count > 0)
    {
        /* 休眠1s */
        sleep(1);
        /* 下单 */
        g_count--;
        cout << "thread1 cnt:" << g_count << endl;
    }

    /* 线程退出 */
    pthread_exit(NULL);
}

void *thread_func2(void *arg)
{
    while (g_count > 0)
    {
        sleep(1);
        /* 下单 */
        g_count--;
        cout << "thread2 cnt:" << g_count << endl;
    }

    /* 线程退出 */
    pthread_exit(NULL);
}

void *thread_func3(void *arg)
{
    while (g_count > 0)
    {
        sleep(1);
        /* 下单 */
        g_count--;
        cout << "thread3 cnt:" << g_count << endl;
    }
    /* 线程退出 */
    pthread_exit(NULL);
}

int main()
{
    pthread_t tid1;
    pthread_create(&tid1, NULL, thread_func1, NULL);

    pthread_t tid2;
    pthread_create(&tid2, NULL, thread_func2, NULL);

    pthread_t tid3;
    pthread_create(&tid3, NULL, thread_func3, NULL);

    // pthread_join(tid1, NULL);
    // pthread_join(tid2, NULL);
    // pthread_join(tid3, NULL);

    while (1)
    {
        sleep(1);
    }

    return 0;
}